// 我的收藏页面 toCollectManageHtml.action
$(function() {

	var IMAGE_BASE_URL = "http://123.207.146.44/house_images";// 访问地址，
	$.ajax({
		type : "post",
		url : "/tenement/getCollectList.action",
		contentType : "application/json;charset=utf-8",
		success : function(json) {
			if (json != "") {
				$.traverseTheData(json);
				bingEvent();
			} else {
				console.log("【查询无果】");
			}
		}
	});
	function bingEvent() {
		// 删除div操作
		$("a[class='delete']").click(function() {
			console.log("---");
			var par = $(this).parent();
			par.find("div").css("display", "block");

		});
		// ×关闭当前div class="confirm"class="cancel"
		$("div[class='cl_close']").click(function() {
			$(this).parent().css("display", "none");
		});
		// 取消操作
		$("a[class='cancel']").click(function() {
			var par = $(this).parent();
			par.parent().css("display", "none");
		});
		// 确认删除操作
		$("a[class='confirm']").click(function() {
			var par = $(this).parent();
			deleteCollect(par);
		});
		// 删除当前tr
	}

	// 确认删除操作
	function deleteCollect(par) {
		var c_id = par.find("input[id='c_id']").attr("value");// 获取当前数据的c_id
		console.log("【获取收藏cId】" + c_id);
		$.ajax({
			type : "post",
			url : "/tenement/deleteCollectUser.action",
			data : c_id,
			contentType : "application/json;charset=utf-8",
			success : function(json) {
				if (json == "1") {
					console.log("删除成功");
					par.parent().css("display", "none");// 删除确认按钮的父级div的父级div隐藏
					window.location.reload();// 刷新当前页面
				}
			},
			error : function(json) {
				console.log("【系统异常】");
			}
		});
	}
	// 加载数据
	$.traverseTheData = function(json) {
		var collect = json.collect;
		var photo = json.photoHouse;
		console.log(collect);
		$(collect)
				.each(
						function(index, element) {
							// index代表下标，element代表对应的内容
							// 在这里判断，赋值
							var hRentalStatus = collect[index].house.hRentalStatus;
							if (hRentalStatus == 0) {
								hRentalStatus = "未出租";
							}
							var innerHtml = "";
							innerHtml = "<tr class='content_cs'>";
							innerHtml += "<td>";
							innerHtml += "<div class='left_imgs clearfix'>"
									+ "<div class='imgs'>"
									+ "<a href='#' target='_blank'><img "
									+ "src='"
									+ IMAGE_BASE_URL
									+ photo[index].pPhotoUrl
									+ photo[index].pPhotoName
									+ "' width='137'"
									+ "height='91'"
									+ "></a>"
									+ "</div>"
									+ "<div class='txt_li'>"
									+ "<p class='p1'>"
									+ "<a href='#'"
									+ "target='_blank'>"
									+ collect[index].house.hCaption
									+ "</a>"
									+ "</p>"
									+ "<p class='p2'>"
									+ collect[index].house.hFloor
									+ "/"
									+ collect[index].house.hFloorTotal
									+ "层 | "
									+ collect[index].house.hSize
									+ "平方米 |"
									+ collect[index].house.hRentalMethod
									+ "</p>" + "</div>" + "</div></td>";
							innerHtml += "<td class='price_c'>"
									+ collect[index].house.hRent
									+ "元/月</td>"
									+ "<td>"
									+ hRentalStatus
									+ "</td>"
									+ "<td>"
									+ collect[index].cCollectdate
									+ "</td>"
									+ "<td>"
									+ "<div class='de_v_box'"
									+ "style='position: relative; display: block'>"
									+ "<a href='javascript:;' class='delete'>删除</a>"
									+ "<div class='delete_box' style='display: none;'>"
									+ "<p class='con_de'>确认要删除该收藏吗？</p>"
									+ "<div class='d_btn'><input type='hidden' id='c_id' value='"
									+ collect[index].cId
									+ "'>"
									+ "<a href='javascript:;' onclick=''"
									+ "class='confirm'>确认</a> <a href='javascript:;'"
									+ "class='cancel'>取消</a>" + "</div>"
									+ "<div class='cl_close'></div></div>"
									+ "</div></td>" + "</tr>";
							$('#tbody').append(innerHtml);
						});
	}

})
